// history.js
Page({
  data: {
    // 页面数据
    historyList: [],
    darkMode: false
  },

  onLoad: function (options) {
    // 页面加载时执行
    this.loadHistory();
    // 检查夜间模式设置
    this.checkDarkMode();
  },

  // 检查夜间模式设置
  checkDarkMode: function () {
    const app = getApp();
    const darkMode = app.globalData.darkMode;
    this.setData({
      darkMode: darkMode
    });
  },

  // 夜间模式变化回调
  onDarkModeChange: function (darkMode) {
    this.setData({
      darkMode: darkMode
    });
  },

  // 加载历史记录
  loadHistory: function () {
    // 从本地存储获取历史记录
    const history = wx.getStorageSync('conversionHistory') || [];
    this.setData({
      historyList: history
    });
  },

  // 清除历史记录
  clearHistory: function () {
    wx.showModal({
      title: '确认清除',
      content: '确定要清除所有历史记录吗？',
      success: (res) => {
        if (res.confirm) {
          wx.removeStorageSync('conversionHistory');
          this.setData({
            historyList: []
          });
          wx.showToast({
            title: '已清除',
            icon: 'success'
          });
        }
      }
    });
  },
  
  // 删除单条历史记录
  deleteHistoryItem: function (e) {
    const index = e.currentTarget.dataset.index;
    const historyList = this.data.historyList;
    
    wx.showModal({
      title: '确认删除',
      content: '确定要删除这条历史记录吗？',
      success: (res) => {
        if (res.confirm) {
          historyList.splice(index, 1);
          this.setData({
            historyList: historyList
          });
          
          // 更新本地存储
          wx.setStorageSync('conversionHistory', historyList);
          
          wx.showToast({
            title: '已删除',
            icon: 'success'
          });
        }
      }
    });
  }
});